/**
 * waterQuality vuex
 */
import posts from '@/plugins/request'
import { Notification } from 'element-ui'
import storage from '@/plugins/localStorage'
// initial state
const state = {
  factorsOption: [
    {
      label: 'pH',
      value: 'pH'
    },
    {
      label: 'COD',
      value: '化学需氧量'
    },
    {
      label: '氨氮',
      value: '氨氮'
    },
    {
      label: '总氮',
      value: '总氮'
    },
    {
      label: '总磷',
      value: '总磷'
    },
    {
      label: '总镍',
      value: '总镍'
    },
    {
      label: '总铜',
      value: '总铜'
    },
    {
      label: '总铬',
      value: '总铬'
    },
    {
      label: '总氰化物',
      value: '总氰化物'
    },
  ],
  stationUsers: [],
  stationOperation: [],
  stationPollutant: [],
  stationListData: [],
  pollutionSourceType: [],
  pollutionListData: {
    data: [],
    total: 0
  },
}
// getters
const getters = {
  getStationUsers: state => state.stationUsers,
  getStationOperation: state => state.stationOperation,
  getStationPollutant: state => {
    if(state.stationPollutant.length) {
      return state.stationPollutant
    }else if(storage.get('pollutantList')) {
      return JSON.parse(storage.get('pollutantList'))
    }else {
      return []
    }
  },
  getFactorsOption: state => state.factorsOption,
  getStationListData: state => state.stationListData,
  getPollutionSourceType: state => state.pollutionSourceType,
  getPollutionListData: state => state.pollutionListData,
}
// actions

const actions = {
  async fetchWaterStationUsers({ commit }) {
    let data = await posts.fetchWaterStationUsers()
    commit('changeStationUsers', data)
  },
  async fetchWaterOperationuser({ commit }) {
    let data = await posts.fetchWaterOperationuser()
    commit('changeStationOperation', data)
  },
  async fetchWaterStationPollutant({ commit }) {
    let data = await posts.fetchWaterStationPollutant()
    storage.set('pollutantList', data)
    commit('changeStationPollutant', data)
  },
  async fetchMonitorSystemSel({ commit }) {
    let data = await posts.fetchMonitorSystemSel()
    return data
  },
  async postWaterStationAdded({ commit }, param) {
    let result = await posts.postWaterStationAdded(param)
    return result
  },
  async fetchWaterStationList({ commit }, param) {
    let data = await posts.fetchWaterStationList(param)
    commit('changeStationListData', data)
  },
  async fetchWaterPollutionSourceType({ commit }) {
    let data = await posts.fetchWaterPollutionSourceType()
    commit('changePollutionSourceType', data)
  },
  async postWaterPollutionAdded({ commit }, param) {
    let result = await posts.postWaterPollutionAdded(param)
    return result
  },
  async postWaterPollutionSupplement({ commit }, param) {
    let result = await posts.postWaterPollutionSupplement(param)
    return result
  },
  async fetchWaterPollutionList({ commit }, param) {
    let result = await posts.fetchWaterPollutionList(param)
    if(result.message !== 'success') {
      Notification.warning({
        title: '错误',
        message: result.info,
        duration: 10000
      })
      commit('changePollutionListData', {successData: [], totalSize: 0})
      return
    }
    commit('changePollutionListData', result.info)
  },
}

// mutations
const mutations = {
  changeStationUsers(state, data) {
    state.stationUsers = data
  },
  changeStationOperation(state, data) {
    state.stationOperation = data
  },
  changeStationPollutant(state, data) {
    state.stationPollutant = data
  },
  changeStationListData(state, data) {
    state.stationListData = data
  },
  changePollutionSourceType(state, data) {
    state.pollutionSourceType = data
  },
  changePollutionListData(state, data) {
    state.pollutionListData.data = data.successData
    state.pollutionListData.total = data.totalSize
  },
}


export default {
  state,
  getters,
  actions,
  mutations
}